package co.mobiless.noterecorder;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ImageCursorAdapter extends SimpleCursorAdapter {

	private Cursor c;
    private Context context;
	
	public ImageCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		this.c = c;
		this.context = context;
	}

	@Override
	public View getView(int pos, View inView, ViewGroup parent) {
	       View v = inView;
	       if (v == null) {
	            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = inflater.inflate(R.layout.fila_notas, null);
	       }
	       this.c.moveToPosition(pos);		
	       String nombre = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_NAME_NOTE));
	       String tiempoIni = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_INITIAL_TIME_NOTE));
	       String textoAdd = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_ADITIONAL_TEXT_NOTE));
	       String rutaImgNota = this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_LOCATION_IMAGE_NOTE))+"/"+
	       		this.c.getString(this.c.getColumnIndex(StorageData.KEY_ROW_NAME_ARCHIVE_IMAGE_NOTE));
	       if (rutaImgNota != null) {
	    	   
	    	   Bitmap b = new BitmapFactory().decodeFile(rutaImgNota);
	    	   
	    	   Log.d("Width&Heigth", b.getWidth()+"&"+b.getHeight());
	    	   
	    	   ImageView iv = (ImageView) v.findViewById(R.id.miniatura_nota);
	    	   //iv.setImageBitmap( Bitmap.createScaledBitmap(BitmapFactory.decodeFile(rutaImgNota), 10, 8, false) );
	    	   iv.setImageBitmap( b);
	       }
            TextView nombreNota = (TextView) v.findViewById(R.id.nombreNota);
            TextView textoAdicional = (TextView) v.findViewById(R.id.textoAdicional);
            TextView tiempoInicial = (TextView) v.findViewById(R.id.initTime);
            nombreNota.setText(nombre);
            textoAdicional.setText(textoAdd.length()>=20?textoAdd.substring(0, 20)+"...":
            	textoAdd==null||textoAdd.length()==0?"--":textoAdd);
            int h = (int) ((Integer.parseInt(tiempoIni) / 1000) / 3600);
            int m = (int) (((Integer.parseInt(tiempoIni) / 1000) / 60) % 60);
            int s = (int) ((Integer.parseInt(tiempoIni) / 1000) % 60);
            tiempoInicial.setText((h<10?"0"+h:h)+":"+(m<10?"0"+m:m)+":"+(s<10?"0"+s:s));
	       return(v);
	}

}
